/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reportcenter.enduser.ui;

import java.awt.Image;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.Action;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Utilities;
import org.openide.util.actions.SystemAction;
import reportcenter.report.business.AdminReportDao;
import reportcenter.report.dominio.report.Report;

/**
 *
 * @author renan
 */
public final class EndUserNode extends AbstractNode {

    private static Report report;
    private static AdminReportDao dao = new AdminReportDao();
    private static final String IMAGE_ICON_BASE = "images/export.png";

    private EndUserNode(Report r) {
        

        super(new ReportChildren(r));
        report = r;
        setIconBaseWithExtension(IMAGE_ICON_BASE);
        setName(r.getTitle() + " - " +r.getSubtitle());

    }
    
    @Override
        public Action[] getActions(boolean context) {
            List allActions = null;
            // a null item here is rendered as an item separator in the menu
            Action[] parentActions = super.getActions(context);
            Action[] myActions = new Action[]{
                null,
                SystemAction.get(ExportXlsAction.class),
            };

            allActions = new ArrayList();
            if (parentActions.length > 0) {
                allActions.addAll(Arrays.asList(parentActions));
            }
            allActions.addAll(Arrays.asList(myActions));
            return (Action[]) allActions.toArray(new Action[0]);
        }

        /** specifies what happens on double-click */
        @Override
        public Action getPreferredAction() {
            return SystemAction.get(ExportXlsAction.class);
        }

        public Image getIcon(int type) {
            return Utilities.loadImage(IMAGE_ICON_BASE);
        }

    public static Node reports() {

        AbstractNode n = new AbstractNode(new ReportChildren(null));
        n.setName("Relatórios Disponíveis");
        return n;

    }

    private static final class ReportChildren extends Children.Keys<Report> {

        Report report;

        public ReportChildren(Report report) {
            this.report = report;
        }

        @Override
        protected void addNotify() {
            if (report == null) {
                Report[] objs = (Report[]) dao.getByRole().toArray(new Report[0]);
                setKeys(objs);
            }
        }

        @Override
        protected Node[] createNodes(Report r) {
            EndUserNode n = new EndUserNode(r);
//            subTitleNode.setDisplayName(r.getTitle() + " - " + r.getSubtitle());

            return new Node[]{n};

        }
    }

//    public static final class ReportNode extends AbstractNode {
//
//        private ReportNode() {
//
//            super(Children.LEAF);
//
//        }
//
//        
//
//        /** 
//         * build the property sheet that is displayed when the properties window is
//         * open and a node of this type is selected.  This is also where we create
//         * the properties for the node.  It works, but I am not sure if it's the best
//         * place to create them,
//         */
//        protected Sheet createSheet() {
//            Sheet s = super.createSheet();
//            Sheet.Set ss = s.get(Sheet.PROPERTIES);
//            if (ss == null) {
//                ss = Sheet.createPropertiesSet();
//                s.put(ss);
//            }
//
//            // add our properties        
//            ss.put(new ItemDescriptionProperty());
//            ss.put(new ItemDueDateProperty());
//
//            // node names are a special case
//            ss.put(new PropertySupport.Name(this));
//
//            return s;
//        }
//
//        /** the property representing our item's description */
//        private class ItemDescriptionProperty extends PropertySupport.ReadWrite {
//
//            public ItemDescriptionProperty() {
//                super("Prop1", String.class, "Description", "A brief description of this item");
//            }
//
//            /** returns the current value of the TodoListItem's description */
//            public Object getValue() throws IllegalAccessException, InvocationTargetException {
//                return "value1";
//            }
//
//            /** changes the current value of the TodoListItem's description */
//            public void setValue(Object newValue) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//                // note that we don't need to set up any listeners here, since any modification
//                // to the item will fire an event to anything listening to changes on that item
//                // (in other words, firePropertyChange() is called in the item's setDesc method)
//                String oldDesc = "old";
////            item.setDesc((String)newValue);
//            }
//        }
//
//        /** the property representing our item's due date */
//        private class ItemDueDateProperty extends PropertySupport.ReadWrite {
//
//            public ItemDueDateProperty() {
//                super("Date1", Date.class, "Due Date", "Date by which this item must be completed");
//            }
//
//            /** returns the current value of the TodoListItem's due date */
//            public Object getValue() throws IllegalAccessException, InvocationTargetException {
//                return new Date();
//            }
//
//            /** changes the current value of the TodoListItem's due date */
//            public void setValue(Object newValue) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//                // commentary in ItemDescriptionProperty.setValue applies here, too
//                Date oldDueDate = new Date();
////            item.setDueDate((Date)newValue);
//            }
//        }
//    }
}
